﻿@model RH.Web.ViewModels.UtilityModels.PagerConfig
@using RH.Web.Utility;
@using RH.Common.Extensions;

@{
    if(Model.UrlFormat.IsNullOrEmpty()){
        throw new ArgumentException("PagerConfig.UrlFormat property must be defined to create pagination UI");
    }
}

@functions{
    int _StartPage = 0;
    int _EndPage = 0;
    int _TotalPage = 0;
    String _PageCountMessage = String.Empty;
    bool linkPrevious = false;
    bool labelPrevious = false;
    bool linkNext = false;
    bool labelNext = false;    
    
    private void BindPager()
    {
        _TotalPage = Convert.ToInt32(Math.Ceiling((double)Model.TotalRecord / (double)Model.PageSize));
        _PageCountMessage = String.Format("Showing Page {0} of {1}", Model.PageNo, _TotalPage);
        if (_TotalPage > 1)
        {
            //int startPage = 0;
            ///Determine the Starting Page to display, calculating 
            ///current page no and total page numbers to show
            if ((Model.PageNo - (Model.NumberOfPageLinksToDisplay / 2)) <= 0)
            {
                _StartPage = 1;
            }
            else
            {
                _StartPage = Model.PageNo - (Model.NumberOfPageLinksToDisplay / 2);
                if ((_TotalPage - _StartPage) < Model.NumberOfPageLinksToDisplay)
                {
                    _StartPage = (_TotalPage - Model.NumberOfPageLinksToDisplay) + 1;
                }
                if (_StartPage <= 0)
                    _StartPage = 1;
            }
            ///Set Visibility for Previous and Next Buttons
            SetVisibilityForPreviousAndNextButtons(_TotalPage);
        }        
    }
    
    public void SetVisibilityForPreviousAndNextButtons(int totalPage)
    {
        if (Model.PageNo == 1)        
            labelPrevious = true;    
        else
            linkPrevious = true;
        if (Model.PageNo == totalPage)
            labelNext = true;
        else        
            linkNext = true;
    }
}

@{
    BindPager();
}


<div class="pager-container">
    <div class="pages"> 
    @if (_TotalPage > 1)
    {
        if (linkPrevious)
        {
            <a href="@String.Format(Model.UrlFormat, Model.PageNo - 1)" style="margin-left:0px;">Previous</a>
        }
        if (labelPrevious)
        {
            <span style="margin-left:0px;">Previous</span>
        }
        for (int i = 1; i <= Model.NumberOfPageLinksToDisplay; i++)
        {
            if (_StartPage > _TotalPage)
            {
                break;
            }
            if (_StartPage == Model.PageNo)
            {
                <span class="current-page">@_StartPage</span>
            }
            else
            {
                <a href="@String.Format(Model.UrlFormat, _StartPage)">@_StartPage</a>
            }
            _StartPage = _StartPage + 1;
        }        
        if (linkNext)
        {
            <a href="@String.Format(Model.UrlFormat, (Model.PageNo + 1))">Next</a>
        }
        if (labelNext)
        {
            <span>Next</span>
        }
    }
    </div>
    <div class="PagerPageCount">@_PageCountMessage</div>
    <div class="clear"></div>
    @if (Model.TotalRecord > 0 && !Model.TotalRecordMessage.IsNullOrEmpty() && Model.TotalRecordMessage.IndexOf("{0}") > -1)
    {
    <div class="PagerTotalRecordMessage">@String.Format(Model.TotalRecordMessage, Model.TotalRecord)</div>
    }
</div>


